import 'package:get/get.dart';
import 'package:ride_rex/main/discover/discover_bean.dart';
import 'package:ride_rex/person_detail/person_detail_logic.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../service/feed_service.dart';
import '../../sqlite/db_manager.dart';

class DiscoverLogic extends GetxController with CommonLife {
  List<FeedUserBean> list = [];

  @override
  void onReady() async {
    super.onReady();
    CommonObservable().addObserver(CommonLogic.keyListenerBlock, block);
    CommonObservable().addObserver(CommonLogic.keyListenerUnblock, unblock);
    CommonObservable().addObserver(CommonLogic.keyListenerFollow, follow);
    CommonObservable().addObserver(CommonLogic.keyListenerUnfollow, unfollow);
    CommonObservable().addObserver(CommonLogic.keyListenerArticleLike, like);
    CommonObservable().addObserver(CommonLogic.keyListenerArticleCancelLike, cancelLike);
  }

  void initData() async {
    Loading.show(clickMaskDismiss: false);

    ///要在数据库打开后执行
    await FeedService.instance.fetchData().whenComplete(() => Loading.dismiss());
    list = await DbManager.getLocalUserList();
    update();
  }

  @override
  void onClose() {
    super.onClose();
    CommonObservable().removeObserver(CommonLogic.keyListenerBlock, block);
    CommonObservable().removeObserver(CommonLogic.keyListenerUnblock, unblock);
    CommonObservable().removeObserver(CommonLogic.keyListenerFollow, follow);
    CommonObservable().removeObserver(CommonLogic.keyListenerUnfollow, unfollow);
    CommonObservable().removeObserver(CommonLogic.keyListenerArticleLike, like);
    CommonObservable().removeObserver(CommonLogic.keyListenerArticleCancelLike, cancelLike);
  }

  ///作品点赞
  void like(key, [value]) {
    String workId = value ?? '';
    for (FeedUserBean e in list) {
      for (FeedUserWorkBean work in (e.works ?? [])) {
        if (work.workId.equals(workId)) {
          work.isLike = '1';
        }
      }
    }
    update();
  }

  ///作品取消点赞
  void cancelLike(key, [value]) {
    String workId = value ?? '';
    for (FeedUserBean e in list) {
      for (FeedUserWorkBean work in (e.works ?? [])) {
        if (work.workId.equals(workId)) {
          work.isLike = '0';
        }
      }
    }
    update();
  }

  void block(key, [value]) {
    String userId = value ?? '';
    list.removeWhere((element) => element.userId == userId);
    update();
  }

  void unblock(key, [value]) async {
    String userId = value ?? '';
    if (userId.empty) return;
    List<FeedUserBean> list = await DbManager.getLocalUserList(userId: userId);
    if (list.isEmpty) return;
    this.list.addAll(list);
    this.list.sort((a, b) => (a.sortId ?? 0).compareTo((b.sortId ?? 0)));
    update();
  }

  void follow(key, [value]) {
    String userId = value ?? '';
    for (var value1 in list) {
      if (value1.userId.equals(userId)) {
        value1.isFriend = true;
      }
    }
    update();
  }

  void unfollow(key, [value]) {
    String userId = value ?? '';
    for (var value1 in list) {
      if (value1.userId.equals(userId)) {
        value1.isFriend = false;
      }
    }
    update();
  }

  @override
  void pageBack() {
    super.pageBack();
  }
}
